package com.edu.admin.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.validation.Valid;

import org.apache.commons.collections.MapUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.edu.admin.entity.SysProduct;
import com.edu.admin.enums.ExceptionEnum;
import com.edu.admin.service.ProductService;
import com.edu.admin.common.RestResult;
import com.edu.admin.vo.ProductVo;
import com.github.pagehelper.PageInfo;

/**
 * 产品管理
 *  
 * @author Wangjiong
 * 
 */
@RestController
@RequestMapping(value = "/product", name = "产品管理")
public class ProductController {
	
	@Autowired
	private ProductService	productService;
	
	
	/**
	 *
	 * @apiGroup product
	 * @api {get} /product/list 获取产品列表
	 * @apiVersion 1.0.0
	 * @apiName getProductList
	 *
	 *
	 *
	 * @apiSuccessExample Success-Response:
	 * HTTP/1.1 200
	 *     {
     *       "code": 200,
     *        "msg": "success",
     *       "data": [
     *       {
     *          "productId": 1,
     *          "productName": "用户管理中心",
     *          "useType": 0
     *       },
     *       {
     *          "productId": 2,
     *          "productName": "互动课堂",
     *          "useType": 0
     *       },
     *      {
     *          "productId": 3,
     *          "productName": "题库中心",
     *          "useType": 0
     *      }
     *      ]
     *    }
	 *
	 *
	 */
    @RequestMapping(value = "list", name = "产品列表", method = RequestMethod.GET)
    @PreAuthorize("hasAuthority('/product/list')")
    public RestResult getProductList() throws Exception {

        List<ProductVo> productList = productService.getProductList();
        return RestResult.ok(productList);
    }
    
    /**
	 * 获取所有产品列表（包括停用产品）
	 * 
	 */
    /**
	 *
	 * @apiGroup product
	 * @api {get} /product/listAll 获取所有产品列表（包括停用产品）
	 * @apiVersion 1.0.0
	 * @apiName getAllProductList
	 *
	 *
	 *
	 * @apiSuccessExample Success-Response:
	 * HTTP/1.1 200
	 *     {
     *       "code": 200,
     *        "msg": "success",
     *       "data": [
     *       {
     *          "productId": 1,
     *          "productName": "练习",
     *          "useType": 0
     *       },
     *       {
     *          "productId": 2,
     *          "productName": "互动课堂",
     *          "useType": 0
     *       },
     *      {
     *          "productId": 3,
     *          "productName": "题库中心",
     *          "useType": 0
     *      }
     *      ]
     *    }
	 *
	 *
	 */
    @RequestMapping(value = "listAll", name = "产品列表", method = RequestMethod.GET)
    @PreAuthorize("hasAuthority('/product/listAll')")
    public RestResult getAllProductList() throws Exception {

        List<ProductVo> productList = productService.getAllProductList();
        return RestResult.ok(productList);
    }
    
    /**
	 *
	 * @apiGroup product
	 * @api {get} /product/list 获取产品列表
	 * @apiVersion 1.0.0
	 * @apiName getProductList
	 *
	 *
	 *
	 * @apiSuccessExample Success-Response:
	 * HTTP/1.1 200
	 *     {
    *       "code": 200,
    *        "msg": "success",
    *       "data": [
    *       {
    *          "productId": 1,
    *          "productName": "用户管理中心",
    *          "useType": 0
    *       },
    *       {
    *          "productId": 2,
    *          "productName": "互动课堂",
    *          "useType": 0
    *       },
    *      {
    *          "productId": 3,
    *          "productName": "题库中心",
    *          "useType": 0
    *      }
    *      ]
    *    }
	 *
	 *
	 */
   @RequestMapping(value = "listSelect", name = "产品列表", method = RequestMethod.GET)
   @PreAuthorize("hasAuthority('/product/listSelect')")
   public RestResult getSelectProductList(@RequestParam(name = "schoolId") Long schoolId) throws Exception {

       List<ProductVo> productList = productService.getSelectProductList(schoolId);
       return RestResult.ok(productList);
   }
    
    /**
   	 *
   	 * @apiGroup product
   	 * @api {get} /product/listAllPage 获取所有产品列表（包括停用产品）（分页显示）
   	 * @apiVersion 1.0.0
   	 * @apiName getAllProductListPage
   	 *
   	 * @apiParam {Integer} pageNum 页码  required = false
   	 * @apiParam {Integer} pageSize 每页行数  required = false
   	 *
   	 * @apiSuccessExample Success-Response:
   	 * HTTP/1.1 200
   	 *     {
	 *	    "code": 200,
	 *	    "msg": "success",
	 *	    "data": {
	 *	        "total": 3,
	 *	        "list": [
	 *	            {
	 *	                "productId": 1,
	 *	                "productName": "用户管理中心",
	 *	                "useType": 0
	 *	            },
	 *	            {
	 *	                "productId": 2,
	 *	                "productName": "互动课堂",
	 *	                "useType": 0
	 *	            },
	 *	            {
	 *	                "productId": 3,
	 *	                "productName": "题库中心",
	 *	                "useType": 0
	 *	            }
	 *	        ],
	 *	        "pageNum": 1,
	 *	        "pageSize": 10,
	 *	        "size": 3,
	 *	        "startRow": 1,
	 *	        "endRow": 3,
	 *	        "pages": 1,
	 *	        "prePage": 0,
	 *	        "nextPage": 0,
	 *	        "isFirstPage": true,
	 *	        "isLastPage": true,
	 *	        "hasPreviousPage": false,
	 *	        "hasNextPage": false,
	 *	        "navigatePages": 8,
	 *	        "navigatepageNums": [
	 *	            1
	 *	        ],
	 *	        "navigateFirstPage": 1,
	 *	        "navigateLastPage": 1,
	 *	        "firstPage": 1,
	 *	        "lastPage": 1
	 *	    }
	 *	}
   	 *
   	 *
   	 */
    @RequestMapping(value = "listAllPage", name = "产品列表", method = RequestMethod.GET)
    @PreAuthorize("hasAuthority('/product/listAllPage')")
    public RestResult getAllProductListPage(@RequestParam(name = "pageNum", required = false) Integer pageNum,
			@RequestParam(name = "pageSize", required = false) Integer pageSize) 
			throws Exception {

		PageInfo<ProductVo> pageInfo = productService.getAllProductListPage(pageNum,pageSize);
        return RestResult.ok(pageInfo);
    }
    
    /**
  	 *
  	 * @apiGroup product
  	 * @api {post} /product/save 保存产品信息
  	 * @apiVersion 1.0.0
  	 * @apiName save
  	 *
  	 * @apiParam {SysProduct} product 产品信息
  	 * @apiParamExample 请求示例（json）
  	 * {
		  "productName":"题库中心"
		
		} 
  	 *
  	 * @apiSuccessExample Success-Response:
  	 * HTTP/1.1 200
		{
		    "code": 200,
		    "msg": "success",
		    "data": {
		        "productId": 3
		    }
		}
  	 *
  	 *
  	 */
	@RequestMapping(value = "save", name = "保存产品信息", method = RequestMethod.POST)
	@PreAuthorize("hasAuthority('/product/save')")
	public RestResult save(@Valid @RequestBody SysProduct product) throws Exception{
		Map<String, Object> result = new HashMap<>();
		
		result = productService.saveProduct(product);
	
		return RestResult.ok(result);
	}
	

	/**
 	 *
 	 * @apiGroup product
 	 * @api {get} /product/info 获取产品信息
 	 * @apiVersion 1.0.0
 	 * @apiName getProduct
 	 *
 	 * @apiParam {Long} productId 产品id
 	 *
 	 * @apiSuccessExample Success-Response:
 	 * HTTP/1.1 200
		{
		    "code": 200,
		    "msg": "success",
		    "data": {
		        "productId": 1,
		        "productName": "用户管理中心",
		        "useType": 0
		    }
		}
 	 *
 	 *
 	 */
	@RequestMapping(value = "info", name = "产品信息", method = RequestMethod.GET)
	@PreAuthorize("hasAuthority('/product/info')")
	public RestResult getProduct(@RequestParam Long productId) throws Exception{
		SysProduct product = null;
		ProductVo productVo = null;
		
		product = productService.selectByPrimaryKey(productId);
		
		if (product!=null)
		{
			productVo = new ProductVo();
			BeanUtils.copyProperties(product,productVo);
		}

		return RestResult.ok(productVo);
	}
	
	/**
	 *
	 * @apiGroup product
	 * @api {post} /product/delete 删除产品
	 * @apiVersion 1.0.0
	 * @apiName deleteProduct
	 *
	 * @apiParam Map<String,Object> paramMap
	 * @apiParamExample 请求示例（json）
  	 * {
		  "productId":"1"
		
		} 
	 *
	 * @apiSuccessExample Success-Response:
	 * HTTP/1.1 200
		{
		    "code": 200,
		    "msg": "success",
		    "data": 1
		}
	 *
	 *
	 */
	@RequestMapping(value="/delete" , method = RequestMethod.POST)
    @PreAuthorize("hasAuthority('/product/delete')")
    public RestResult deleteProduct(@RequestBody Map<String,Object> dataMap) throws Exception {
    	   
       	Long productId = MapUtils.getLong(dataMap, "productId");
       	
       	if (productId == null || productId<=0)
       	{
       		return RestResult.error(ExceptionEnum.BADREQUEST.getCode(),ExceptionEnum.BADREQUEST.getMsg());
       	}
    	    			
    	int isOk = productService.deleteProduct(productId);
    	if (isOk>0)
		{
			return RestResult.ok(isOk);
		}
		else
		{
			return RestResult.error();
		}
    }
    
    /**
	 *
	 * @apiGroup product
	 * @api {post} /product/on 启用产品
	 * @apiVersion 1.0.0
	 * @apiName onProduct
	 *
	 * @apiParam Map<String,Object> paramMap
	 * @apiParamExample 请求示例（json）
  	 * {
		  "productId":"1"
		
		} 
	 *
	 * @apiSuccessExample Success-Response:
	 * HTTP/1.1 200
		{
		    "code": 200,
		    "msg": "success",
		    "data": 1
		}
	 *
	 *
	 */
	@RequestMapping(value="/on" , method = RequestMethod.POST)
    @PreAuthorize("hasAuthority('/product/on')")
    public RestResult onProduct(@RequestBody Map<String,Object> dataMap) throws Exception {
 	   
    	Long productId = MapUtils.getLong(dataMap, "productId");
    	
    	if (productId == null || productId<=0)
    	{
    		return RestResult.error(ExceptionEnum.BADREQUEST.getCode(),ExceptionEnum.BADREQUEST.getMsg());
    	}
    	    			
    	int isOk = productService.useProduct(productId,0);
    	if (isOk>0)
		{
			return RestResult.ok(isOk);
		}
		else
		{
			return RestResult.error();
		}
    }
    
    /**
	 *
	 * @apiGroup product
	 * @api {post} /product/off 停用产品
	 * @apiVersion 1.0.0
	 * @apiName offProduct
	 *
	 * @apiParam Map<String,Object> paramMap
	 * @apiParamExample 请求示例（json）
  	 * {
		  "productId":"1"
		
		} 
	 *
	 * @apiSuccessExample Success-Response:
	 * HTTP/1.1 200
		{
		    "code": 200,
		    "msg": "success",
		    "data": 1
		}
	 *
	 *
	 */
	@RequestMapping(value="/off" , method = RequestMethod.POST)
    @PreAuthorize("hasAuthority('/product/off')")
    public RestResult offProduct(@RequestBody Map<String,Object> dataMap) throws Exception {
 	   
    	Long productId = MapUtils.getLong(dataMap, "productId");
    	
    	if (productId == null || productId<=0)
    	{
    		return RestResult.error(ExceptionEnum.BADREQUEST.getCode(),ExceptionEnum.BADREQUEST.getMsg());
    	}
    	    			
    	int isOk = productService.useProduct(productId,1);
    	if (isOk>0)
		{
			return RestResult.ok(isOk);
		}
		else
		{
			return RestResult.error();
		}
    }


}
